package  com.syqc.sensor.tdemp

import androidx.activity.viewModels
import androidx.core.view.isVisible
import autodispose2.autoDispose
import com.gyf.immersionbar.ktx.immersionBar
import com.syqc.comlib.base.BaseActivity
import com.syqc.comlib.utils.showToast
import com.syqc.comlib.utils.startActivityTo
import com.syqc.utils.Keys
import com.syqc.entity.OilDay
import com.syqc.monitor.R
import  com.syqc.sensor.AddLeakActivity
import com.syqc.utils.DataUtil
import com.syqc.utils.ToolsUtil
import com.syqc.utils.Units
import com.syqc.monitor.chart.MPChartUtils
import com.syqc.monitor.databinding.ActivityRecentTempBinding

/**
 *@author  hfjs
 *@time   2025/2/25 8:51
 *@desc
 */
class TempChartActivity :
    BaseActivity<ActivityRecentTempBinding>(ActivityRecentTempBinding::inflate) {
    private var mData: OilDay? = null
    private val mViewModel by viewModels<TempChartVM>()
    private lateinit var mUnit: String

    //是否优化
    private var isOpti = true

    //是否里程
    private var isKm = false
    override fun initView() {
        mData = DataUtil.stats
        plate = intent.getStringExtra(Keys.INTENT_PLATE) ?: ""
        val teamName = intent.getStringExtra(Keys.INTENT_TEAM_NAME) ?: ""
        val unitTime = getString(R.string.unit_num)
        mData?.run {
            mUnit = unit
            binding.tvToolbarTitle.text = "$plate  $name  $day"
            immersionBar { titleBar(binding.rlToolbar) }

            binding.lrvTempTime.setRightText(day)
            binding.lrvTempTeam.setRightText(teamName)
            binding.lrvTempDown.setRightText("$downTimes$unitTime  $downTotal$unit")
            binding.lrvTempDown.nextVisible(downTimes > 0)

            binding.lrvTempUp.setRightText("$upTimes$unitTime  $upTotal$unit")
            binding.lrvTempUp.nextVisible(upTimes > 0)

            binding.lrvTempMile.setRightText(if (mile > 1000) "$mileKm ${Units.km}" else "$mile ${Units.m}")

            mViewModel.handleAddLeak(list)
            mViewModel.handleData(DataUtil.oilPints!!)
        }

        MPChartUtils.initTempLineChart(binding.tempLineChart, "#FF0000", "#00618C", "#5A6B01", mUnit)

        mViewModel.oilDetail().observe(this) {
            MPChartUtils.initXAxis(binding.tempLineChart, mViewModel.timeData)
            MPChartUtils.setLineDataSet2(
                binding.tempLineChart,
                mViewModel.details,
                it[0],
                it[1],
                it[2],
                it[3],
                getString(R.string.desc_temp),
                mUnit
            ).isOpt = isOpti
            MPChartUtils.changeLineData(binding.tempLineChart, isOpti)
        }
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
    }


    override fun setListener() {
        binding.ivToolbarStart.setOnClickListener {
            finish()
        }

        binding.tvToolbarAction.setOnClickListener {
            if (binding.llTempState.isVisible) {
                binding.llTempState.isVisible = false
                binding.tvToolbarAction.setText(R.string.desc_com_display)
            } else {
                binding.llTempState.isVisible = true
                binding.tvToolbarAction.setText(R.string.desc_com_hide)
            }
        }

        binding.tvOilOpti.setOnClickListener {
            isOpti = !isOpti
            binding.tvOilOpti.isSelected = isOpti
            MPChartUtils.changeLineData(binding.tempLineChart, isOpti, isKm)
        }

        binding.tvLineSpeed.setOnClickListener {
            isKm = false
            binding.tvLineSpeed.isChecked = true
            MPChartUtils.changeLineData(binding.tempLineChart, isOpti, isKm)
        }

        binding.tvLineMile.setOnClickListener {
            isKm = true
            binding.tvLineMile.isChecked = true
            MPChartUtils.changeLineData(binding.tempLineChart, isOpti, isKm)
        }
        binding.lrvTempDown.setOnClickListener {
            if (ToolsUtil.isFastClick()) return@setOnClickListener
            mData?.let {
                if (it.downTimes > 0) {
                    if (mViewModel.leakDetail.isNullOrEmpty()) {
                        showToast(R.string.desc_toast_no_data)
                        return@setOnClickListener
                    }
                    DataUtil.addLeak = mViewModel.leakDetail
                    startActivityTo<AddLeakActivity>(this) {
                        putExtra(Keys.INTENT_TITLE, plate)
                        putExtra(Keys.INTENT_OTHER, mUnit)
                        putExtra(Keys.INTENT_TYPE, 6)
                    }
                }
            }
        }
        binding.lrvTempUp.setOnClickListener {
            if (ToolsUtil.isFastClick()) return@setOnClickListener
            mData?.let {
                if (it.upTimes > 0) {
                    if (mViewModel.addDetail.isNullOrEmpty()) {
                        showToast(R.string.desc_toast_no_data)
                        return@setOnClickListener
                    }
                    DataUtil.addLeak = mViewModel.addDetail
                    startActivityTo<AddLeakActivity>(this) {
                        putExtra(Keys.INTENT_TITLE, plate)
                        putExtra(Keys.INTENT_OTHER, mUnit)
                        putExtra(Keys.INTENT_TYPE, 5)
                    }
                }
            }
        }

    }

    private var plate: String = ""


    override fun onDestroy() {
        super.onDestroy()

        MPChartUtils.clearData()
    }

}